package myJs.myPkg.reactHookForm

import japgolly.scalajs.react.CtorType.ChildArg
import japgolly.scalajs.react.facade.React
import japgolly.scalajs.react.{Children, JsComponent, JsFnComponent}
import myJs.Implicits.FieldValue
import myJs.myPkg.rSelect.SelectOption
import slinky.readwrite.ObjectOrWritten

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport
import scala.scalajs.js.|

/** Created by yz on 20/9/2023
  */
object FormProvider {

  @JSImport("react-hook-form", "FormProvider")
  @js.native
  object RawComponent extends js.Object

  case class Props(
      trigger: js.UndefOr[String => Unit] = js.undefined,
      control: js.UndefOr[Control] = js.undefined,
      formState: js.UndefOr[FormStateRT] = js.undefined,
      setValue: js.UndefOr[
        (
            String,
            js.UndefOr[String | js.Array[String] | Boolean | SelectOption],
            js.UndefOr[ObjectOrWritten[SetValueOptions]]
        ) => Unit
      ] = js.undefined,
      watch: js.UndefOr[String => FieldValue] = js.undefined,
      getFieldState: js.UndefOr[String => FieldStateT] = js.undefined
  )

  val Component = JsFnComponent[ObjectOrWritten[Props], Children.Varargs](RawComponent)

  def apply(
      trigger: js.UndefOr[String => Unit] = js.undefined,
      control: js.UndefOr[Control] = js.undefined,
      formState: js.UndefOr[FormStateRT] = js.undefined,
      setValue: js.UndefOr[
        (
            String,
            js.UndefOr[String | js.Array[String] | Boolean | SelectOption],
            js.UndefOr[ObjectOrWritten[SetValueOptions]]
        ) => Unit
      ] = js.undefined,
      watch: js.UndefOr[String => FieldValue] = js.undefined,
      getFieldState: js.UndefOr[String => FieldStateT] = js.undefined
  )(children: ChildArg*) =
    Component(Props(trigger, control, formState, setValue, watch, getFieldState))(children: _*)

}
